#include "HelpBoard.h"
#include "AudioManage.h"

static const char* BUTTON_CLOSE = "SettingBoard/help_exit.png";
static const char* PNG_POINT_W = "HelpBoard/point_unselect.png";
static const char* PNG_POINT_Y = "HelpBoard/point_select.png";
static const int HELP_IMAGE_COUNT = 4;

bool HelpBoard::init()
{
	if (Layer::init() == false)
	{
		return false;
	}

	Size viewSize = Director::getInstance()->getVisibleSize();

	LayerColor* pBaseColor = LayerColor::create(Color4B(0, 0, 0, 150), viewSize.width * 2, viewSize.height * 2);
	pBaseColor->setPosition(viewSize.width / 2 * -1, viewSize.height / 2 * -1);
	addChild(pBaseColor);

	Sprite* pBoardBaseSprite = Sprite::create("SettingBoard/settingBase.png");
	pBoardBaseSprite->setPosition(viewSize.width / 2, viewSize.height * 0.5f);
	addChild(pBoardBaseSprite);

	m_pHelpImage = Sprite::create("HelpBoard/help1.png");
	m_nIndex = 1;
	m_pHelpImage->setPosition(pBoardBaseSprite->getContentSize().width / 2,
							  pBoardBaseSprite->getContentSize().height / 2);
	pBoardBaseSprite->addChild(m_pHelpImage);

	m_rgPosition.clear();
	for (int i = 0; i < HELP_IMAGE_COUNT; i++)
	{
		Sprite* p = Sprite::create(PNG_POINT_W);
		Point pos = Point(pBoardBaseSprite->getContentSize().width * 0.38 + i * 50, 
						  pBoardBaseSprite->getContentSize().height * 0.07f);

		m_rgPosition.push_back(pos);
		p->setPosition(pos);
		pBoardBaseSprite->addChild(p, 1);
	}

	m_pSelectedP = Sprite::create(PNG_POINT_Y);
	m_pSelectedP->setPosition(m_rgPosition.at(0));
	pBoardBaseSprite->addChild(m_pSelectedP);

	MenuItem* pCloseButton = MenuItemImage::create(BUTTON_CLOSE, BUTTON_CLOSE, CC_CALLBACK_0(HelpBoard::exitHelp, this));
	pCloseButton->setPosition(pBoardBaseSprite->getContentSize().width * 0.98f, pBoardBaseSprite->getContentSize().height * 0.98f);
	pCloseButton->setScale(1.5f);

	Menu* pButtonMenu = Menu::create(pCloseButton, nullptr);
	pButtonMenu->setPosition(Point::ZERO);
	pBoardBaseSprite->addChild(pButtonMenu);

	auto listener1 = EventListenerTouchOneByOne::create();
	listener1->setSwallowTouches(true);
	listener1->onTouchBegan = CC_CALLBACK_2(HelpBoard::onHelpTouchBegin, this);
	listener1->onTouchEnded = CC_CALLBACK_2(HelpBoard::onHelpTouchEnd, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);

	setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
	setTouchEnabled(true);
	setSwallowsTouches(true);

	return true;
}

void HelpBoard::nextStep()
{
	if (m_bPlayingAnimation)
	{
		return;
	}

	m_nIndex++;
	m_nIndex = MIN(m_nIndex, HELP_IMAGE_COUNT);
	m_bPlayingAnimation = true;

	string strPath = __String::createWithFormat("HelpBoard/help%d.png", m_nIndex)->getCString();
	std::function<void()> funChangeImage = [this, strPath]()
	{
		m_pHelpImage->setTexture(strPath);
	};
	std::function<void()> funAnimationEnd = [this]()
	{
		m_bPlayingAnimation = false;
		m_pSelectedP->setPosition(m_rgPosition.at(m_nIndex - 1));
	};

	m_pHelpImage->runAction(Sequence::create(FadeOut::create(0.1f),
		CallFunc::create(funChangeImage),
		FadeIn::create(0.1f), 
		CallFunc::create(funAnimationEnd),
		nullptr));

	AudioManage::buttonSound();
}

void HelpBoard::lastStep()
{
	if (m_bPlayingAnimation)
	{
		return;
	}

	m_nIndex--;
	m_nIndex = MAX(m_nIndex, 1);
	m_bPlayingAnimation = true;

	string strPath = __String::createWithFormat("HelpBoard/help%d.png", m_nIndex)->getCString();
	std::function<void()> funChangeImage = [this, strPath]()
	{
		m_pHelpImage->setTexture(strPath);
	};
	std::function<void()> funAnimationEnd = [this]()
	{
		m_bPlayingAnimation = false;
		m_pSelectedP->setPosition(m_rgPosition.at(m_nIndex - 1));
	};

	m_pHelpImage->runAction(Sequence::create(FadeOut::create(0.1f),
		CallFunc::create(funChangeImage),
		FadeIn::create(0.1f),
		CallFunc::create(funAnimationEnd),
		nullptr));

	AudioManage::buttonSound();
}

void HelpBoard::exitHelp()
{
	AudioManage::button2Sound();
	removeFromParent();
}

bool HelpBoard::onHelpTouchBegin(Touch* touch, Event* event)
{
	m_startPos = touch->getLocation();

	return true;
}

void HelpBoard::onHelpTouchEnd(Touch* touch, Event* event)
{
	m_endPos = touch->getLocation();

	if (m_endPos.x > m_startPos.x)
	{
		lastStep();
	}
	else if (m_endPos.x < m_startPos.x)
	{
		nextStep();
	}
}